package com.czy.owner.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.RechargeEntity;
import com.czy.owner.utils.MyLog;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by wangyang on 2016/10/9
 */
public class ReChangeAdapter extends BaseRecyclerAdapter<RechargeEntity, ReChangeAdapter.ReChangeHolder> {
    private int selectedPostion = -1;
    private int whiteColor = Color.parseColor("#FFFFFF");
    private int themeColor = Color.parseColor("#09c0c7");
    private int balackColor = Color.parseColor("#484847");
    private int yellowColor = Color.parseColor("#f4a624");

    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");

    public ReChangeAdapter(Context context, List<RechargeEntity> data) {
        super(context, data);
    }

    public void setSelectedPostion(int selectedPostion) {
        this.selectedPostion = selectedPostion;
    }

    public int getSelectedPostion() {
        return selectedPostion;
    }


    @Override
    public void onBindViewHolder(ReChangeHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.setData(data.get(position), data.get(position).getShowDataType());
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getShowDataType();
    }


    @Override
    public ReChangeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new ReChangeHolder(layoutInflater.inflate(R.layout.item_store_recharge, parent, false), viewType);
        } else {
            return new ReChangeHolder(layoutInflater.inflate(R.layout.item_recharge_last, parent, false), viewType);
        }

    }

    public class ReChangeHolder extends BaseRecyclerAdapter.ViewHolder {
        private LinearLayout llRechargeBg;
        private TextView tvRechargeAmount;
        private TextView tvGiveAmount;
        private LinearLayout llRechargeLastBg;
        private ImageView imgRecharge;
        private TextView tvCustomer;

        public ReChangeHolder(View itemView, int viewType) {
            super(itemView);
            if (viewType == 0) {
                llRechargeBg = (LinearLayout) itemView.findViewById(R.id.ll_recharge_bg);
                tvRechargeAmount = (TextView) itemView.findViewById(R.id.tv_recharge_amount);
                tvGiveAmount = (TextView) itemView.findViewById(R.id.tv_give_amount);
            } else {
                llRechargeLastBg = (LinearLayout) itemView.findViewById(R.id.ll_recharge_last_bg);
                imgRecharge = (ImageView) itemView.findViewById(R.id.img_recharge);
                tvCustomer = (TextView) itemView.findViewById(R.id.tv_customer);
            }


        }

        //ContextCompat.getDrawable(mContext, R.mipmap.icon_xuanzegou)
        public void setData(RechargeEntity data, int viewType) {
            if (viewType == 0) {
                double amount = data.getAmount();
                double giveAmount = data.getGivingAmount();

//                int mAmount =(int)amount;
//                int mGiveAmount = (int)giveAmount;

                tvRechargeAmount.setText(decimalFormat.format(amount) + "元");
                if (giveAmount == 0) {
                    tvGiveAmount.setVisibility(View.GONE);
                } else {
                    tvGiveAmount.setText("送" + decimalFormat.format(giveAmount));
                }


                if (selectedPostion == getAdapterPosition()) {
                    llRechargeBg.setBackgroundResource(R.drawable.shape_recharge_select_bg);
                    tvRechargeAmount.setTextColor(whiteColor);
                    tvGiveAmount.setTextColor(whiteColor);
                } else {
                    llRechargeBg.setBackgroundResource(R.drawable.shape_recharge_normal_bg);
                    tvRechargeAmount.setTextColor(yellowColor);
                    tvGiveAmount.setTextColor(balackColor);
                }
            } else {
                if (selectedPostion == getAdapterPosition()) {
                    llRechargeLastBg.setBackgroundResource(R.drawable.shape_recharge_select_bg);
                    tvCustomer.setTextColor(whiteColor);
                    imgRecharge.setBackgroundResource(R.mipmap.icon_customer);

                } else {
                    llRechargeLastBg.setBackgroundResource(R.drawable.shape_recharge_normal_bg);
                    tvCustomer.setTextColor(themeColor);
                    imgRecharge.setBackgroundResource(R.mipmap.icon_customer_check);

                }
            }

        }
    }


}
